package servlets1;

import beans1.Officebean;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import service1.OfficeService;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;

import java.util.ArrayList;
@WebServlet("/Main_Office")
public class Main_Office extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        System.out.println("in Main.java,the action is "+action);
        if("delOffice".equals(action))
        {
            System.out.println("i am in delOffice");
            delOffice(request, response);
        }

        else if("editOffice".equals(action)) {
            System.out.println("i am in editOffice");
            try {
				editOffice(request, response);
			} catch (ParseException | IOException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            System.out.println("i am out editOffice");
        } else if ("insertOffice".equals(action)) {
            System.out.println("i am in insertOffice");
            try {
                insertOffice(request,response);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        } else
        {
            System.out.println("i am in showlist");
            showlist(request, response);
        }
    }

    private void insertOffice(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ParseException {
        OfficeService officeService=new OfficeService();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        Officebean str=new Officebean(id, name);
        System.out.println("完成insertoffice函数");
        officeService.insertOffice(str);
        response.sendRedirect(request.getContextPath()+"/Main_Office");
    }

    private void editOffice(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException, SQLException {
        OfficeService officeService=new OfficeService();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        Officebean str=new Officebean(id,name);
        officeService.editOffice(str);
        response.sendRedirect(request.getContextPath()+"/Main_Office");
    }

    private void delOffice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        OfficeService officeService=new OfficeService();
        Officebean str=new Officebean();
        str.setID(request.getParameter("id"));
        str.setNAME(request.getParameter("name"));
        int result=officeService.delOffice(str);
        if(result==1) {
            System.out.println("success");
        }
        else {
            System.out.println("false");
        }
        response.sendRedirect(request.getContextPath()+"/Main_Office");
    }

    private void showlist(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        OfficeService officeService=new OfficeService();
        ArrayList<Officebean> Officebeans = officeService.browse();
        request.setAttribute("list", Officebeans);
        request.getRequestDispatcher("admin/officeManage.jsp").forward(request,response);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
